<template>
  <div class="app-container">
    <el-form :model="batchForm" :rules="rules" ref="formRef" >
      <el-form-item label="学习批次" prop="studyBatch"  label-width="100px">
        <el-input v-model="batchForm.studyBatch" readonly style="width: 220px;"></el-input>
      </el-form-item>

      <el-form-item label="开始日期" prop="startDate" label-width="100px">
        <el-date-picker v-model="batchForm.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="结束日期" prop="endDate" label-width="100px">
        <el-date-picker v-model="batchForm.endDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>

      <el-form-item style="text-align: center">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import { getLastBatchBySkillsId,getBatchById,editBatch } from '@/api/regulatory/studySkills'

export default {
  name: 'editSkillsBatch',
  props: ["skillsId","batchId"],
  created() {
    if(this.batchId !=='') {
      getBatchById(this.batchId).then(res=> {
        this.batchForm = res.data;
      })
    }else {
      getLastBatchBySkillsId(this.skillsId).then(res=> {
        if(res.data === null) {
          this.batchForm.studyBatch = 1;
        }else {
          this.batchForm.studyBatch = res.data.studyBatch+1;
        }
      })
    }
  },
  data() {
    var checkEndDate = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('结束日期不能为空'));
      } else if (value < this.batchForm.startDate) {
        callback(new Error('结束日期需大于开始日期'));
      } else {
        callback();
      }
    };

    return {
      batchForm : {
        id : null,
        studySkillsId : this.skillsId,
        studyBatch : '',
        startDate : '',
        endDate : ''
      },
      rules: { //表单验证
        startDate: [
          {required: true, message: '开始日期不能为空', trigger: 'blur'}
        ],
        endDate: [
          {required: true,validator : checkEndDate, trigger: 'blur'}
        ]
      },
    }
  },
  methods : {
    //表单提交
    submitForm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          editBatch(this.batchForm).then(()=> {
            this.$message.success('保存成功！');
            this.$emit('editBatchSuccess');
          })
        }
      });
    },
  }
}
</script>



<style scoped>

</style>
